/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDOptionsFrame.java
 *
 * Created on April 10, 2006, 7:28 PM
 */

package ptd;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.io.File;

/**
 *
 * @author  John
 */
public class PTDOptionsFrame extends javax.swing.JFrame {
    
    /** Creates new form PTDOptionsFrame */
    public PTDOptionsFrame() {

        initComponents();
        
        ImageIcon a = PTDImageLoader.getImageIcon(PTDImageLoader.IMG_PTD_ICON);
        setIconImage(a.getImage());

        SuiteLabel.setText("Default Workspace");        
        SuiteBrowse.setText("...");
        DirectoryLabel.setText("Default Directory");
        DirectoryBrowse.setText("...");
        //AgentLabel.setText("Default Agent");        
        //AgentBrowse.setText("...");
        //AgentPortLabel.setText("Agent Port");
        LicenseLabel.setText("License Key");
        //MachineLabel.setText("Default Machine");
        
        WorkspaceOption.setText("");
        //AgentOption.setText("");
        
        ButtonOK.setText("OK");
        ButtonCancel.setText("Cancel");
        
        this.setSize(480, 170);
        
        putValues();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SizeLabel = new javax.swing.JLabel();
        WorkspaceOption = new javax.swing.JTextField();
        ButtonOK = new javax.swing.JButton();
        ButtonCancel = new javax.swing.JButton();
        SuiteBrowse = new javax.swing.JButton();
        SuiteLabel = new javax.swing.JLabel();
        LicenseLabel = new javax.swing.JLabel();
        LicenseKey = new javax.swing.JTextField();
        DirectoryLabel = new javax.swing.JLabel();
        DirectoryOption = new javax.swing.JTextField();
        DirectoryBrowse = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Options");
        setAlwaysOnTop(true);
        setResizable(false);
        getContentPane().setLayout(null);

        WorkspaceOption.setText("jTextField1");
        getContentPane().add(WorkspaceOption);
        WorkspaceOption.setBounds(170, 40, 240, 19);

        ButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonOKActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonOK);
        ButtonOK.setBounds(100, 100, 100, 20);

        ButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonCancel);
        ButtonCancel.setBounds(260, 100, 100, 20);

        SuiteBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SuiteBrowseActionPerformed(evt);
            }
        });
        getContentPane().add(SuiteBrowse);
        SuiteBrowse.setBounds(420, 40, 33, 20);
        getContentPane().add(SuiteLabel);
        SuiteLabel.setBounds(10, 40, 150, 20);
        getContentPane().add(LicenseLabel);
        LicenseLabel.setBounds(10, 10, 100, 20);
        getContentPane().add(LicenseKey);
        LicenseKey.setBounds(170, 10, 240, 19);
        getContentPane().add(DirectoryLabel);
        DirectoryLabel.setBounds(10, 70, 100, 20);

        DirectoryOption.setText("jTextField1");
        getContentPane().add(DirectoryOption);
        DirectoryOption.setBounds(170, 70, 240, 19);

        DirectoryBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DirectoryBrowseActionPerformed(evt);
            }
        });
        getContentPane().add(DirectoryBrowse);
        DirectoryBrowse.setBounds(420, 70, 33, 20);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SuiteBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SuiteBrowseActionPerformed
        String sFile = openFile("pws", "PTD Workspace (.pws)");
        if(sFile.length() > 0){
            WorkspaceOption.setText(sFile);
        }
        
    }//GEN-LAST:event_SuiteBrowseActionPerformed

    private void ButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelActionPerformed
        dispose();
    }//GEN-LAST:event_ButtonCancelActionPerformed

    private void ButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonOKActionPerformed
        getValues();
        dispose();
    }//GEN-LAST:event_ButtonOKActionPerformed

private void DirectoryBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DirectoryBrowseActionPerformed
        String sFile = openDir();
        if(sFile.length() > 0){
            DirectoryOption.setText(sFile);
        }
}//GEN-LAST:event_DirectoryBrowseActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PTDOptionsFrame().setVisible(true);
            }
        });
    }
    
    public String openFile(String sFilter, String sFilterText){
        String Ret = "";
        JFileChooser aChooser = new JFileChooser();
        PTDFileFilter filter = new PTDFileFilter(sFilter, sFilterText);
        aChooser.addChoosableFileFilter(filter);
        aChooser.setCurrentDirectory(PTDMain.g_CurrentDir);

        int result = aChooser.showOpenDialog(this);
        if(result == JFileChooser.CANCEL_OPTION){
            return Ret;
        }
        
        try {            
            File afile = aChooser.getSelectedFile();
            PTDMain.g_CurrentDir = afile.getParentFile();
            return afile.getAbsolutePath();
        }catch(Exception e){
            Main.debug_out(e.getMessage());
        }
        return Ret;
    }
    
    public String openDir(){
        String Ret = "";
        JFileChooser aChooser = new JFileChooser();
        aChooser.setCurrentDirectory(PTDMain.g_CurrentDir);
        aChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);    

        int result = aChooser.showOpenDialog(this);
        if(result == JFileChooser.CANCEL_OPTION){
            return Ret;
        }
        PTDMain.g_CurrentDir = aChooser.getSelectedFile();
        Ret = aChooser.getSelectedFile().getAbsolutePath();
        return Ret;
    }    
    
    public void getValues(){
        PTDOptions.m_DefaultWorkspace = WorkspaceOption.getText();
        PTDOptions.m_DefaultDirectory = DirectoryOption.getText();
        //PTDOptions.m_DefaultAgent = AgentOption.getText();
        //PTDOptions.m_DefaultAgentPort = AgentPort.getText();
        PTDOptions.m_LicenseKey = LicenseKey.getText();
        //PTDOptions.m_DefaultMachine = MachineOption.getText();        
        
        PTDOptions.saveToFile(PTDOptions.getOptionsFile());
        
        PTDRegister aReg = new PTDRegister();
        boolean bReg = aReg.CheckKey(PTDOptions.m_LicenseKey);
        if(bReg == false){
            //PTDRegistrationFrame aFrame = new PTDRegistrationFrame(true);
            //aFrame.setVisible(true);
            PTDMessageBox.MessageBox("Invalid Registration Key.  Continuing Evaluation...");
            Main.getMainFrame().bEvaluationMode = true;
        }
        // machine name
    }
    
    public void putValues(){
        WorkspaceOption.setText(PTDOptions.m_DefaultWorkspace);
        DirectoryOption.setText(PTDOptions.m_DefaultDirectory);
        //AgentOption.setText(PTDOptions.m_DefaultAgent);
        //AgentPort.setText(PTDOptions.m_DefaultAgentPort);
        LicenseKey.setText(PTDOptions.m_LicenseKey);
        //MachineOption.setText(PTDOptions.m_DefaultMachine);
        
        // options to add
        // Default Machine
        // Suite Output Header/Footer File
        // Testcase Output Header/Footer File
        // Suite Output File (perhaps should go in suite/testcase panel)
        // License Key (how to enable 30-day trial... special 30-day trial key? - Generated on website by request?)
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCancel;
    private javax.swing.JButton ButtonOK;
    private javax.swing.JButton DirectoryBrowse;
    private javax.swing.JLabel DirectoryLabel;
    private javax.swing.JTextField DirectoryOption;
    private javax.swing.JTextField LicenseKey;
    private javax.swing.JLabel LicenseLabel;
    private javax.swing.JLabel SizeLabel;
    private javax.swing.JButton SuiteBrowse;
    private javax.swing.JLabel SuiteLabel;
    private javax.swing.JTextField WorkspaceOption;
    // End of variables declaration//GEN-END:variables
    
}
